<template>
  <div class="menu-content" :style="menuHeight">
    <ta-menu mode="horizontal" :data="menuList" :props="props" :selectedKeys="selectedKey" @click="clickMenu" ref="menuHorizon" class="top-menu" :style="menuHeight"></ta-menu>
    <ta-menu mode="horizontal" :data="moreList" :props="props" :selectedKeys="selectedKey" @click="clickMenu" v-if="hiddenIndex" class="more-menu" :style="{...menuHeight, left: moreLeft + 'px'}"></ta-menu>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
export default {
  data () {
    return {
      moreLeft: 0,
      hiddenIndex: 0,
      moreList: [{name: '更多', icon: 'appstore', id: 'menuMore', children: []}],
      props: {
        key: 'id',
        title: 'name'
      }
    }
  },
  computed: {
    ...mapGetters({
      state: 'getStateValue'
    }),
    selectedKey () {
      let result = ''
      const {activeMenuOne, activeMenuTwo, menuType} = this.state
      result = menuType == 'top' ? activeMenuTwo : activeMenuOne
      return [result || '']
    },
    menuList () {
      let result = []
      const {menuList, menuType} = this.state
      if (menuType == 'top') {
        result = menuList
      } else if (menuType == 'leftTop') {
        let temp = JSON.parse(JSON.stringify(menuList))
        temp.forEach((item) => {
          delete item.children
          result.push(item)
        })
      }
      return result
    },
    menuHeight () {
      const { headerHeight } = this.state
      let height = parseInt(headerHeight)
      return {
        height: height + 'px',
        lineHeight: height - 2 + 'px'
      }
    }
  },
  mounted () {
    window.addEventListener('resize', () => {
      this.setValue()
    }, false)
  },
  updated () {
    this.setValue()
  },
  methods: {
    setValue () {
      if (this.state.menuType != 'left') {
        let liList = this.$refs.menuHorizon.$el.children, moreLeft = 0, hiddenIndex = 0
        for (let i = 0; i < liList.length; i++) {
          let li = liList[i].getBoundingClientRect()
          if (li.top == 0) {
            moreLeft += li.width
          } else {
            hiddenIndex = i; break
          }
        }
        this.moreLeft = moreLeft
        this.hiddenIndex = hiddenIndex
        if (hiddenIndex) {
          this.moreList[0].children = this.menuList.slice(hiddenIndex)
        }
      }
    },
    clickMenu ({key, keyPath}) {
      const {menuType, menuList, barType} = this.state
      let breadcrumb = []
      if (menuType == 'top') {
        this.$store.dispatch('setActiveMenu', {level: 'one', menuId: keyPath[keyPath.length - 1]})
        this.$store.dispatch('setActiveMenu', {level: 'two', menuId: key})
        let tabMenu = menuList
        for (let i = keyPath.length - 1; i > -1; i--) {
          if (keyPath[i] != 'menuMore') {
            tabMenu = tabMenu.filter(item => item.id == keyPath[i])[0]
            if (barType == 'breadcrumb') {
              breadcrumb.push(tabMenu.name)
            }
            tabMenu = i ? tabMenu.children : tabMenu
          }
        }
        if (breadcrumb.length) {
          this.$store.dispatch('setStateValue', {breadcrumb: breadcrumb})
        }
        this.$store.dispatch('addTabMenuList', {value: tabMenu})
      } else if (menuType == 'leftTop') {
        this.$store.dispatch('setActiveMenu', {level: 'one', menuId: key})
      }
    }
  }
}
</script>
<style type="text/scss" lang="scss">
  body, html { padding: 0; margin: 0; }
  .menu-content { width: 100%; overflow: hidden; position: relative;
    .top-menu { width: 100%; padding-right: 100px;
      &.ant-menu-horizontal { border-bottom: none }
    }
    .more-menu { width: 100px; position: absolute; top: 0; left: 0;
      &.ant-menu-horizontal { border-bottom: none }
    }
  }
</style>
